package com.jumei.flume.processor.rules;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import com.jumei.flume.bo.SensorMessage;
import com.jumei.flume.processor.ProcessorContext;

import java.util.HashMap;
import java.util.List;

import static com.jumei.flume.common.StringUtils.safe4Param;

/**
 * 点击曝光来源页面预处理
 * 
 * https://echo.int.jumei.com/issues/158296
 * 
 * @author admin
 *
 */
public class AddClickNameRules {
	public final static HashMap<String, TypeResolver2> name_resolvers = new HashMap<String, TypeResolver2>();

	static {

		try {
			final List<String> names = Resources.readLines(Resources.getResource("name.txt"), Charsets.UTF_8);
			final List<List<String>> namePairs = Lists.transform(names, new Function<String, List<String>>() {
				@Override
				public List<String> apply(String input) {
					if (Strings.isNullOrEmpty(input) || input.startsWith("#"))
						return null;
					
					return Splitter.on("|").splitToList(input);
				}

			});
			
			for(final List<String> r : namePairs) {
				if(null == r) continue;
				
				name_resolvers.put(r.get(1), new TypeResolver2() {

					@Override
					public Type resolve(SensorMessage record, ProcessorContext context) {
						Type t = new Type();
						t.setCnName(r.get(1));
						t.setEnName(r.get(0));
						
						return t;
					}

					@Override
					public boolean filter(SensorMessage record, ProcessorContext context) {
						List<String> temp = Lists.newArrayList("click_material","view_material");
						String material_page = record.getMaterial_page();
						return temp.contains(record.getEvent()) && split2List(r.get(2)).contains(safe4Param(material_page, ""));
					}
					
					private List<String> split2List(String page) {
						return Splitter.on(",").splitToList(page);
					}
				});
			}
			
			//3
			name_resolvers.put("搜索结果页-商品", new TypeResolver2() {

				@Override
				public Type resolve(SensorMessage record, ProcessorContext context) {
					Type t = new Type();
					t.setCnName("搜索结果页-商品");
					t.setEnName("product_search_list");
					
					return t;
				}

				@Override
				public boolean filter(SensorMessage record, ProcessorContext context) {
					List<String> temp = Lists.newArrayList("click_material","view_material");
					String material_page = record.getMaterial_page();
					return temp.contains(record.getEvent()) 
							&& ( "product_search_list".equals(material_page) );
				}
				
				
			});
			
			//43
			name_resolvers.put("消息中心", new TypeResolver2() {

				@Override
				public Type resolve(SensorMessage record, ProcessorContext context) {
					Type t = new Type();
					t.setCnName("消息中心");
					t.setEnName("messagebox");
					
					return t;
				}

				@Override
				public boolean filter(SensorMessage record, ProcessorContext context) {
					List<String> temp = Lists.newArrayList("click_material","view_material");
					String material_page = record.getMaterial_page();
					return temp.contains(record.getEvent()) && 
							( !Strings.isNullOrEmpty(material_page) && material_page.contains("jumeimall://page/messagebox") );
				}
				
				
			});
			
			
		} catch (Exception e) {
			
		}
	}
}
